import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		hasJms: false,
		userInfo: {
			id: '',
			nickName: '',
			userName: '',
			phone: '',
			headimg: '',
			openId: '',
			token: '',
			typeId: 1,
			jmsStatus: 0,
			jmsName: '',
			userTypeText: '',
			plotId: 0,
			plotName: ''
		},

	},
	mutations: {
		login(state, userInfo) {
			state.hasLogin = true;
			if (userInfo.typeId == 2) {
				state.hasJms = true
			}
			state.userInfo.id = userInfo.id || '';
			state.userInfo.nickName = userInfo.nickName || '';
			state.userInfo.userName = userInfo.userName || '';
			state.userInfo.phone = userInfo.phone || '';
			state.userInfo.headimg = userInfo.headimg || '';
			state.userInfo.openId = userInfo.openId || '';
			state.userInfo.token = userInfo.token || '';
			state.userInfo.typeId = userInfo.typeId || 1;
			state.userInfo.jmsStatus = userInfo.jmsStatus || 0;
			state.userInfo.plotId = userInfo.plotId || 0;
			state.userInfo.plotName = userInfo.plotName || '';
			// if (userInfo.jmsStatus == 1)
			// 	state.userInfo.userTypeText = '设计师'
			// if (userInfo.jmsStatus == 2)
			// 	state.userInfo.userTypeText = '项目经理'
			// if (userInfo.jmsStatus == 3)
			// 	state.userInfo.userTypeText = '监理'
			// if (userInfo.jmsStatus == 4)
			// 	state.userInfo.userTypeText = '材料商'
			state.userInfo.jmsName = userInfo.jmsName || '';
			uni.setStorage({
				key: 'userInfo',
				data: userInfo
			})
			// console.log(state.userInfo);
		},
		updatePlot(state, userInfo) {
			state.userInfo.id = userInfo.id || state.userInfo.id;
			state.userInfo.nickName = userInfo.nickName || state.userInfo.nickName;
			state.userInfo.userName = userInfo.userName || state.userInfo.userName;
			state.userInfo.phone = userInfo.phone || state.userInfo.phone;
			state.userInfo.headimg = userInfo.headimg || state.userInfo.headimg;
			state.userInfo.openId = userInfo.openId || state.userInfo.openId;
			state.userInfo.token = userInfo.token || state.userInfo.token;
			state.userInfo.typeId = userInfo.typeId || state.userInfo.typeId;
			state.userInfo.jmsStatus = userInfo.jmsStatus || state.userInfo.jmsStatus;
			state.userInfo.plotId = userInfo.plotId || state.userInfo.plotId;
			state.userInfo.plotName = userInfo.plotName || state.userInfo.plotName;
			console.log("state.userInfo", state.userInfo)
			console.log("userInfo", userInfo)
			uni.setStorage({
				key: 'userInfo',
				data: state.userInfo
			})
		},
		logout(state) {
			state.hasLogin = false;
			state.hasJms = false
			state.userInfo = {};
			uni.removeStorage('userInfo')
		}
	},
	actions: {

	}
})

export default store